package com.example.campus_activity_backend.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.campus_activity_backend.common.ErrorCode;
import com.example.campus_activity_backend.domain.Student;
import com.example.campus_activity_backend.domain.TeachingStudentInClass;
import com.example.campus_activity_backend.exception.BusinessException;
import com.example.campus_activity_backend.service.ClassService;
import com.example.campus_activity_backend.service.StudentService;
import com.example.campus_activity_backend.service.TeachingStudentInClassService;
import com.example.campus_activity_backend.mapper.TeachingStudentInClassMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Hongxia
* @description 针对表【t_teaching_student_in_class】的数据库操作Service实现
* @createDate 2023-11-20 10:35:45
*/
@Service
public class TeachingStudentInClassServiceImpl extends ServiceImpl<TeachingStudentInClassMapper, TeachingStudentInClass>
    implements TeachingStudentInClassService {

    @Autowired
    private StudentService studentService;


    @Override
    public Boolean addStudenToClass(List<Integer> studentIdList, Integer classId) {
        if(studentIdList == null || classId == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Student> students = studentService.listByIds(studentIdList);
        for(Student student : students){
            TeachingStudentInClass sc = new TeachingStudentInClass();
            sc.setStudentId(student.getId());
            sc.setClassId(classId);
            boolean saveResult = this.save(sc);
            if(!saveResult){
                throw new BusinessException(ErrorCode.SYSTEM_ERROR,"学员id"+student.getId()+"关联失败");
            }
        }
        return true;
    }
}




